import Layout from '../layout/Layout.vue'
import { createRouter, createWebHistory } from "vue-router";

/**
 * 路由配置说明：
 * hidden: true                  如果设置为 true，则该路由不会显示在侧边栏中（默认 false）
 * alwaysShow: true              如果设置为 true，则始终显示根菜单，无论子路由数量
 * redirect: noRedirect          如果设置为 noRedirect，则不会在面包屑中重定向
 * name: 'router-name'            路由名称，用于 <keep-alive>（必须设置）
 * meta: {
 *   title: 'title'              显示在侧边栏和面包屑中的名称（建议设置）
 *   icon: 'svg-name'            显示在侧边栏中的图标
 * }
 */
export const constantRoutes = [
  {
    path: '/Login',
    name: 'Login',
    component: () => import("@/views/Login"),
    hidden: true // 不在侧边栏显示
  },
  {
    path: '/',
    component: Layout,
    redirect: '/home',
    children: [
      {
        path: 'home',
        name: '主页',
        component: () => import("@/views/Home"),
        meta: { title: '主页', icon: 'home' }
      }
    ]
  },
  {
    path: '/system',
    component: Layout,
    alwaysShow: true,
    name: '系统管理',
    meta: { title: '系统管理', icon: 'system' },
    children: [
      {
        path: 'sysRoleList',
        name: '角色管理',
        component: () => import("@/views/system/sysRoleList"),
        meta: { title: '角色管理', icon: 'role' }
      },
      {
        path: 'sysMenuList',
        name: '权限管理',
        component: () => import("@/views/system/sysMenuList"),
        meta: { title: '权限管理', icon: 'menu' }
      }
    ]
  },
  {
    path: '/',
    component: Layout,
    alwaysShow: true,
    name: '用户管理',
    meta: { title: '用户管理', icon: 'system' },
    children: [
      {
        path: 'stuInfo',
        name: '学生信息',
        component: () => import("@/views/StuInfo"),
        meta: { title: '学生信息', icon: 'user' }
      },
      {
        path: 'dormManagerInfo',
        name: '宿舍管理员信息',
        component: () => import("@/views/DormManagerInfo"),
        meta: { title: '宿舍管理员信息', icon: 'manager' }
      },
      {
        path: 'teacherInfo',
        name: '辅导员信息',
        component: () => import("@/views/teacherInfo"),
        meta: { title: '辅导员信息', icon: 'manager' }
      }
    ]
  },
  {
    path: '/system',
    component: Layout,
    alwaysShow: true,
    name: '系统管理',
    meta: { title: '系统管理', icon: 'system' },
    children: [
      {
        path: 'sysRoleList',
        name: '角色管理',
        component: () => import("@/views/system/sysRoleList"),
        meta: { title: '角色管理', icon: 'role' }
      },
      {
        path: 'sysMenuList',
        name: '权限管理',
        component: () => import("@/views/system/sysMenuList"),
        meta: { title: '权限管理', icon: 'menu' }
      }
    ]
  },


  {
    path: '/',
    component: Layout,
    alwaysShow: true,
    name: '角色管理',
    meta: { title: '角色管理', icon: 'dorm' },
    children: [
      {
        path: 'stuInfo',
        name: '学生信息',
        component: () => import("@/views/@/views/StuInfo"),
        meta: { title: '学生信息', icon: 'user' }
      },
      {
        path: 'dormManagerInfo',
        name: '宿舍管理员信息',
        component: () => import("@/views/DormManagerInfo"),
        meta: { title: '宿舍管理员信息', icon: 'manager' }
      },
      {
        path: 'buildingInfo',
        name: '楼宇信息',
        component: () => import("@/views/BuildingInfo"),
        meta: { title: '楼宇信息', icon: 'building' }
      },
      {
        path: 'roomInfo',
        name: '房间信息',
        component: () => import("@/views/RoomInfo"),
        meta: { title: '房间信息', icon: 'room' }
      },
      {
        path: 'noticeInfo',
        name: '公告信息',
        component: () => import("@/views/NoticeInfo"),
        meta: { title: '公告信息', icon: 'notice' }
      },
      {
        path: 'adjustRoomInfo',
        name: '调宿申请',
        component: () => import("@/views/AdjustRoomInfo"),
        meta: { title: '调宿申请', icon: 'adjust' }
      },
      {
        path: 'repairInfo',
        name: '报修信息',
        component: () => import("@/views/RepairInfo"),
        meta: { title: '报修信息', icon: 'repair' }
      },
      {
        path: 'visitorInfo',
        name: '访客管理',
        component: () => import("@/views/VisitorInfo"),
        meta: { title: '访客管理', icon: 'visitor' }
      },
      {
        path: 'WaterInfo',
        name: '水费管理',
        component: () => import("@/views/WaterInfo"),
        meta: { title: '水费管理', icon: 'water' }
      },
      {
        path: 'ElectricInfo',
        name: '电费管理',
        component: () => import("@/views/ElectricInfo"),
        meta: { title: '电费管理', icon: 'electric' }
      },
      {
        path: 'CheckInfo',
        name: '卫生检查',
        component: () => import("@/views/CheckInfo"),
        meta: { title: '卫生检查', icon: 'check' }
      },
      {
        path: 'applyAbsentRecord',
        name: '管理缺寝记录',
        component: () => import("@/views/ApplyAbsentRecord"),
        meta: { title: '管理缺寝记录', icon: 'record' }
      }
    ]
  },

  {
    path: '/personal',
    component: Layout,
    redirect: '/personal/selfInfo',
    name: '个人中心',
    meta: { title: '个人中心', icon: 'personal' },
    children: [
      {
        path: 'selfInfo',
        name: '个人信息',
        component: () => import("@/views/SelfInfo"),
        meta: { title: '个人信息', icon: 'info' }
      },
      {
        path: 'myRoomInfo',
        name: '我的宿舍',
        component: () => import("@/views/MyRoomInfo"),
        meta: { title: '我的宿舍', icon: 'room' }
      },
      {
        path: 'applyRepairInfo',
        name: '报修申请',
        component: () => import("@/views/ApplyRepairInfo"),
        meta: { title: '报修申请', icon: 'repair' }
      },
      {
        path: 'applyChangeRoom',
        name: '申请调宿',
        component: () => import("@/views/ApplyChangeRoom"),
        meta: { title: '申请调宿', icon: 'adjust' }
      }
    ]
  },
  {
    path: '/chat',
    component: Layout,
    redirect: '/chat/lm',
    name: '聊天',
    meta: { title: '聊天', icon: 'chat' },
    children: [
      {
        path: 'lm',
        name: '聊天室',
        component: () => import("@/views/lm"),
        meta: { title: '聊天室', icon: 'message' }
      }
    ]
  },
  {
    path: '/404',
    name: '404',
    component: () => import("@/views/404"),
    hidden: true
  },
  {
    path: '*',
    redirect: '/404',
    hidden: true
  }
];

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes: constantRoutes
});

// 路由守卫
router.beforeEach((to, from, next) => {
  const user = window.sessionStorage.getItem('user');
  if (to.path === '/Login') {
    return next();
  }
  if (!user) {
    return next('/Login');
  }
  if (to.path === '/' && user) {
    return next('/home');
  }
  next();
});

export default router;